/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**********************************************************************
 * Copyright (c) 2014 Pieter Wuille                                   *
 * Distributed under the MIT software license, see the accompanying   *
 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
 **********************************************************************/


#ifndef _SECP256K1_HASH_
#define _SECP256K1_HASH_

#include <string.h>
#include "ql_type.h"

// -----------------------
// sha256 definition
// -----------------------
typedef struct {
    ql_uint32_t   s[32];
    ql_uint32_t buf[16]; /* In big endian */
    ql_size_t   bytes;
} ql_sha256_t;

ql_void_t sha256_init (ql_sha256_t* hash);
ql_void_t sha256_write(ql_sha256_t* hash, const ql_byte_t* data, ql_size_t len);
ql_void_t sha256_final(ql_sha256_t* hash, ql_byte_t* out32);

// -----------------------
// hmac definition
// -----------------------
typedef struct {
    ql_sha256_t inner, outer;
} ql_hmac_t;

ql_void_t hmac_init (ql_hmac_t* hash, const ql_byte_t* key , ql_size_t keylen);
ql_void_t hmac_write(ql_hmac_t* hash, const ql_byte_t* data, ql_size_t size);
ql_void_t hmac_final(ql_hmac_t* hash, ql_byte_t* out32);

// -----------------------
// rfc6979 definition
// -----------------------
typedef struct {
    ql_byte_t v[32];
    ql_byte_t k[32];
    ql_size_t retry;
} ql_rfc6979_t;

ql_void_t rfc6979_init (ql_rfc6979_t* rng, const ql_byte_t* key, ql_size_t keylen);
ql_void_t rfc6979_gen  (ql_rfc6979_t* rng, ql_byte_t* out, ql_size_t outlen);
ql_void_t rfc6979_final(ql_rfc6979_t* rng);
ql_void_t rfc6979_nonce(ql_byte_t* nonce32, const ql_byte_t* msg32, const ql_byte_t* key32, const ql_byte_t* algo16, ql_void_t *data, ql_size_t counter);

#endif
